import cv2

def draw_detections(image, detections, padding=50):
    """
    在图像上绘制检测框和标签
    :param image: 输入图像
    :param detections: [(x1, y1, x2, y2, conf, label), ...]
    :param padding: 如果检测是在填充后图像上做的，这里要减去padding
    :return: 绘制后的图像
    """
    for x1, y1, x2, y2, conf, label in detections:
        # 坐标校正
        x1_adj = int(x1 - padding)
        y1_adj = int(y1 - padding)
        x2_adj = int(x2 - padding)
        y2_adj = int(y2 - padding)
        # 可选：确保坐标不越界
        x1_adj = max(x1_adj, 0)
        y1_adj = max(y1_adj, 0)
        x2_adj = min(x2_adj, image.shape[1] - 1)
        y2_adj = min(y2_adj, image.shape[0] - 1)
        # 绘制
        cv2.rectangle(image, (x1_adj, y1_adj), (x2_adj, y2_adj), (0, 255, 0), 2)
        text = f'{label}: {conf:.2f}'
        cv2.putText(image, text, (x1_adj, y1_adj - 10),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.7, (36, 255, 12), 2)
    return image


def save_image(image, filename):
    cv2.imwrite(filename, image)
